﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace _4.DownloadFileAndPrintIfExistExceptions
{
    class DownloadFileAndPrintIfExistExceptions
    {

        static void Main(string[] args)
        {

            try
            {
                Console.WriteLine("input direct url:");
                //string URL = "http://www.contoso.com/library/homepage/images/ms-banner.gif";
                //string URL = Console.ReadLine() ;
                string URL = Console.ReadLine();
                string[] URLparts = URL.Split('/');

                string fileName = URLparts[URLparts.Length - 1];

                Console.WriteLine("URL : {0} \n fileName : {1}", URL, fileName);

                //string fileName = "ms-banner.gif", 
                string myStringWebResource = null;
                // Create a new WebClient instance.
                WebClient myWebClient = new WebClient();
                // Concatenate the domain with the Web resource filename.
                myStringWebResource = URL;
                Console.WriteLine("Downloading File {0} from \"{1}\" .......\n\n", fileName, myStringWebResource);
                // Download the Web resource and save it into the current filesystem folder.
                myWebClient.DownloadFile(myStringWebResource, fileName);
                Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
            }
            catch (System.Net.WebException webex)
            {
                Console.WriteLine("Web Exception found! Message : {0}", webex.Message);
            }
            catch (System.Net.HttpListenerException httplistenerex)
            {
                Console.WriteLine("Http Listener exception! Message : {0}", httplistenerex.Message);
            }
            catch (System.NullReferenceException nullex)
            {
                Console.WriteLine("Null is not true value! Message : {0}", nullex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Other exception: {0}", ex.Message);
            }
            finally
            {
                Console.WriteLine("End of Program!");
            }
        }
    }
}